import { ref, onMounted, onUnmounted } from 'vue'
import { verifyPassword, verifyWithdraw } from '@/utils/validateTools'
import pinia from '@/store/index'
import { storeToRefs } from 'pinia'
import { User } from '@/store/user'
import router from '@/router/index'
import { i18n } from '@/languages/index.ts'
import { NetPacket } from '@/netBase/NetPacket'
import PKwebsocket from '@/lolo/Ws'
import eventBus from '@/lolo/eventBus'
import { updateStorePayPwd } from '@/utils/useStoreMethods.ts'
const t = i18n.global.t
const userInfo = User(pinia)
export const { info, roleInfo } = storeToRefs(userInfo)
export const securityshowToast = ref(false)
export const toast_value = ref('')
const isModify = ref(false) // 是否修改
export const verifyTrue = ref(false) // 验证通过
export const emailVerifyTrue = ref(false) // 邮箱验证通过
export const isMobile = ref(false) // 当前修改的是电话还是邮箱
const paramsData: any = ref({}) // 记录提交参数

export const passwordValidator = (val: any) => {
  if (!val) {
    return Promise.reject(t('home_page_enterPassword'))
  } else if (!verifyPassword(val)) {
    return Promise.reject(t('home_page_passwordFormatIncorrect'))
  } else {
    return Promise.resolve()
  }
}
// 资金密码验证
export const withdrawpasswordValidator = (val: any) => {
  if (!val) {
    return Promise.reject(t('home_page_enterPassword'))
  } else if (!verifyWithdraw(val)) {
    return Promise.reject(t('input_tips_password1'))
  } else {
    return Promise.resolve()
  }
}

export const routineFormData: any = ref({
  old_password: '',
  new_password: '',
  new_password_confirm: '',
})

export const routineRef: any = ref(null)

const useHooks = () => {
  const emailFormData = ref({
    old_password: '',
    new_password: '',
    new_password_confirm: '',
    codeValue: '',
  })
  //   const formRef = ref(null)

  const notify_result = (rs: any) => {
    if (rs.code == 1) {
      if (isModify.value) {
        isModify.value = false
        verifyTrue.value = isMobile.value
        emailVerifyTrue.value = !isMobile.value
      } else {
        securityshowToast.value = true
        toast_value.value = t('operation_success')
        setTimeout(() => {
          router.push('/securitySettings')
        }, 2000)
      }
    } else {
      securityshowToast.value = true
      toast_value.value = t(rs.message)
    }
  }

  // 资金密码修改回执
  const notify_withdraw_result = (rs: any) => {
    if (rs.code == 1) {
      routineFormData.value.new_password &&
        updateStorePayPwd(routineFormData.value.new_password)

      if (isModify.value) {
        isModify.value = false
        verifyTrue.value = isMobile.value
        emailVerifyTrue.value = !isMobile.value
      } else {
        securityshowToast.value = true
        toast_value.value = t('operation_success')
        setTimeout(() => {
          router.push('/securitySettings')
        }, 2000)
      }
    } else {
      securityshowToast.value = true
      toast_value.value = t(rs.message)
    }
  }

  onMounted(() => {
    securityshowToast.value = false
    eventBus.on(
      'msg_notify_bind_or_modify_withdraw_password',
      notify_withdraw_result
    )
    eventBus.on('msg_notify_reset_account_password', notify_result)
  })

  onUnmounted(() => {
    verifyTrue.value = false
    routineFormData.value.old_password = ''
    routineFormData.value.new_password = ''
    routineFormData.value.new_password_confirm = ''
    eventBus.off(
      'msg_notify_bind_or_modify_withdraw_password',
      notify_withdraw_result
    )
    eventBus.off('msg_notify_reset_account_password', notify_result)
  })

  return {
    routineFormData,
    emailFormData,
    info,
    roleInfo,
    routineRef,
    passwordValidator,
    withdrawpasswordValidator,
    changePassword,
    verifyTrue,
    isModify,
    changewithdrawPassword,
  }
}

export default useHooks

// 更改密码或验证验证码
// operate_type: 2, //1表示点击下一步需要校验手机或者邮箱是否已经绑定，验证码是否正确；operate_type=2表示立即修改密码
// modify_type: 1, //1表示常规修改密码，modify_type=2表示通过手机修改密码，modify_type=3表示通过邮箱修改密码
export const changePassword = (params: any, type: number, name: string) => {
  let req = NetPacket.req_reset_account_password()
  // 点击下一步验证手机或邮箱是否绑定
  if (type == 1) {
    isModify.value = true
    req.operate_type = 1
  } else {
    isModify.value = false
    req.operate_type = 2
  }
  if (name == 'mobile') {
    req.username = params.codeValue + params.mobile
    req.captcha = params.verify_code
  }
  if (name == 'email') {
    req.username = params.email
    req.captcha = params.verify_code
  }
  isMobile.value = name == 'mobile'
  req.modify_type = params.modify_type
  // req.username = info.value?.full_name
  req.old_password = params?.old_password
  req.new_password = params?.new_password
  req.new_password_confirm = params?.new_password_confirm
  PKwebsocket.instance.send(req, true)
}

// 更改资金密码或验证验证码
//operate_type=1表示绑定资金密码；operate_type=2表示常规修改资金密码；operate_type=3表示通过手机修改密码；operate_type=4表示通过邮箱修改密码
export const changewithdrawPassword = (
  params: any,
  type: number,
  name: string
) => {
  console.log(6666666622)

  paramsData.value = { ...params }
  let req = NetPacket.req_bind_or_modify_withdraw_password()
  // 点击下一步验证手机或邮箱是否绑定
  if (type == 1) {
    isModify.value = true
  } else {
    isModify.value = false
  }
  if (name == 'mobile') {
    req.mobile_or_email = params.codeValue + params.mobile
    req.captcha = params.verify_code
  }
  if (name == 'email') {
    req.mobile_or_email = params.email
    req.captcha = params.verify_code
  }
  isMobile.value = name == 'mobile'
  req.operate_type = params.operate_type
  // req.username = info.value?.full_name
  req.old_password = params?.old_password
  req.new_password = params?.new_password
  req.new_password_confirm = params?.new_password_confirm
  req.role_id = Number(roleInfo.value?.id)
  req.step = params.step
  PKwebsocket.instance.send(req, true)
}
